#include "pch.h"
#include "addVector.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>

using namespace std;

void vector_add_cpu(const float* src_a,
	const float* src_b,
	float* res,
	const int num)
{
	for (int i = 0; i < num; i++)
		res[i] = src_a[i] + src_b[i];
}

int convertToString(const char* filename, std::string& s)
{
	size_t size;
	char* str;
	std::fstream f(filename, (std::fstream::in | std::fstream::binary));
	if (f.is_open())
	{
		size_t fileSize;
		f.seekg(0, std::fstream::end);
		size = fileSize = (size_t)f.tellg();
		f.seekg(0, std::fstream::beg);
		str = new char[size + 1];
		if (!str)
		{
			f.close();
			return NULL;
		}
		f.read(str, fileSize);
		f.close();
		str[size] = '\0';
		s = str;
		delete[] str;
		return 0;
	}
	printf("Error: Failed to open file %s\n", filename);
	return 1;
}

int getPlatforms(cl_platform_id* platforms, cl_uint* num_platforms)
{
	cl_uint num_entries = 0;
	cl_uint num_platforms;
	cl_platform_id* platforms;
	cl_int r0 = clGetPlatformIDs(0, NULL, num_platforms);

	if (r0 != CL_SUCCESS) {
		return 1;
	}
	if (num_platforms <= 0) {
		return 1;
	}
	platforms = (cl_platform_id*)malloc(*num_platforms * sizeof(cl_platform_id));
	cl_int r0 = clGetPlatformIDs(*num_platforms, platforms, NULL);

	/*cl_platform_id platform0 = platforms[0];
	free(platforms);*/
	return 0;
}

int getDevices(cl_platform_id platform, cl_device_id* devices, cl_uint* num_device)
{
	cl_uint num_device;
	cl_int r1 = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, num_device);
	if (r1 != CL_SUCCESS) {
		return 1;
	}
	if (num_device <= 0) {
		return 1;
	}
	devices = (cl_device_id*)malloc(sizeof(cl_device_id) * *num_device);
	cl_int r1 = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, *num_device, devices, NULL);
	return 0;
}


void run_add_Vector() 
{
	cl_uint num_platforms, num_device;
	cl_platform_id* platforms;
	cl_device_id* devices;
	if (getPlatforms(platforms, &num_platforms))
		return;
	if (num_platforms <= 0)
		return;
	if (getDevices(platforms[0], devices, &num_device)) 
		return;
}
